package view

import (
	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/data/binding"
	"fyne.io/fyne/v2/layout"
	"fyne.io/fyne/v2/theme"
	"fyne.io/fyne/v2/widget"
)

//type:1=监控,2=地址
type Setting struct {
	Contract      string
	Address       string
	Type          int8
	MonitorMin    int
	MonitorNotify int
	NotifyMethod  int8
}

func Monitor(win fyne.Window) (fyne.CanvasObject, Setting) {
	var setting Setting
	setting.Contract = ""
	setting.Address = ""
	//合约和地址卡片
	contractLabel := widget.NewLabel("合约")
	contractEntry := widget.NewEntryWithData(binding.BindString(&setting.Contract))
	contractEntry.PlaceHolder = "为空则查询全部"
	contractForm := container.New(layout.NewFormLayout(), contractLabel, contractEntry)

	addressLabel := widget.NewLabel("地址")
	addressEntry := widget.NewEntryWithData(binding.BindString(&setting.Address))
	addressEntry.PlaceHolder = "为空则查询全部"
	addressForm := container.New(layout.NewFormLayout(), addressLabel, addressEntry)

	firstRow := container.NewGridWithColumns(2, contractForm, addressForm)
	firstCard := widget.NewCard("", "", firstRow)

	setting.MonitorMin = 100
	setting.MonitorNotify = 1000
	setting.NotifyMethod = 0
	//交易监控设置项卡片
	transferMonitorMinLabel := widget.NewLabel("监控最小数量")
	transferMonitorMinEntry := widget.NewEntryWithData(binding.IntToString(binding.BindInt(&setting.MonitorMin)))
	transferMonitorMinForm := container.New(layout.NewFormLayout(), transferMonitorMinLabel, transferMonitorMinEntry)

	transferMonitorNotifyLabel := widget.NewLabel("通知最小数量")
	transferMonitorNotifyEntry := widget.NewEntryWithData(binding.IntToString(binding.BindInt(&setting.MonitorNotify)))
	transferMonitorNotifyForm := container.New(layout.NewFormLayout(), transferMonitorNotifyLabel, transferMonitorNotifyEntry)

	transferMonitorMethodLabel := widget.NewLabel("通知方式")
	var transferMonitorMethodData = []string{0: "逐单通知", 1: "合并通知"}
	transferMonitorMethodSelect := widget.NewSelect(transferMonitorMethodData, func(s string) {
		for i, x := range transferMonitorMethodData {
			if s == x {
				setting.NotifyMethod = int8(i)
				break
			}
		}
	})
	transferMonitorMethodSelect.SetSelected(transferMonitorMethodData[0])
	transferMonitorMethodForm := container.New(layout.NewFormLayout(), transferMonitorMethodLabel, transferMonitorMethodSelect)

	transferMonitorRow := container.NewGridWithColumns(3, transferMonitorMinForm, transferMonitorNotifyForm, transferMonitorMethodForm)
	transferMonitorCard := widget.NewCard("", "", transferMonitorRow)

	//地址采集设置项卡片
	addressCollectionLabel := widget.NewLabel("地址采集设置项待确定")
	addressCollectionCard := widget.NewCard("", "", addressCollectionLabel)
	addressCollectionCard.Hidden = true

	//数据显示卡片
	transferData := container.NewVBox()
	collectionData := container.NewVBox()
	transferCard := widget.NewCard("", "", container.NewVScroll(transferData))
	collectionCard := widget.NewCard("", "", container.NewVScroll(collectionData))
	collectionCard.Hidden = true

	setting.Type = 1
	//设置TAB按钮
	StartBtn := widget.NewButtonWithIcon("开始", theme.MediaPlayIcon(), func() {})
	StartBtn.CreateRenderer()
	StopBtn := widget.NewButtonWithIcon("停止", theme.MediaStopIcon(), func() {})
	StartBtn.OnTapped = func() {
		StartBtn.Icon = theme.MediaPlayIcon()
		StartBtn.Hidden = true
		StopBtn.Hidden = false
	}
	StopBtn.Hidden = true
	StopBtn.OnTapped = func() {
		StartBtn.Hidden = false
		StopBtn.Icon = theme.MediaStopIcon()
		StopBtn.Hidden = true
	}
	settingButtons := container.NewGridWithColumns(3,
		widget.NewButton("交易监控", func() {
			setting.Type = 1
			transferMonitorCard.Hidden = false
			transferCard.Hidden = false
			addressCollectionCard.Hidden = true
			collectionCard.Hidden = true
		}),
		widget.NewButton("地址采集", func() {
			setting.Type = 2
			transferMonitorCard.Hidden = true
			transferCard.Hidden = true
			addressCollectionCard.Hidden = false
			collectionCard.Hidden = false
		}),
		StartBtn, StopBtn,
	)

	return container.NewVBox(firstCard, settingButtons, transferMonitorCard, addressCollectionCard, transferCard, collectionCard), setting
}
